package com.zht.demo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Test05 {
    public static void main(String[] args) throws Exception{
        String name = "张三";
        String pwd = "11";
        boolean flag = login(name,pwd);
        System.out.println(flag);
    }
    public static boolean login(String name,String pwd) throws Exception{
        // 连接数据库，去查找  stu 表，有没有name = name ，pwd = pwd。

        //2.注册驱动
        Class.forName("com.mysql.jdbc.Driver");//mysql5之后的驱动jar包可以省略注册驱动的步骤
        // 3. 获取数据库连接对象 Connection
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1", "root", "root");


        Statement statement = conn.createStatement();

        // 5 .定义sql
        // select * from stu where name ='zhangsan' and pwd = 'xxx';
       // String sql = "select * from stu where  name =  " + name +" and pwd =" + pwd ;
        String sql = "select * from stu where  name = '"+name+"'  and pwd = '"+pwd+"'";

        ResultSet resultSet = statement.executeQuery(sql); // 得到时ResultSet
        return resultSet.next(); // 得到结果集，往下取一行，如果得到true ，说明
    }
}
